from numpy import genfromtxt

from hand import Hand
from mapping import Map
from actions import Actions
import numpy as np
from visualization import vis
import matplotlib.pyplot as plt

class Convert:
    use_counting = True
    
    if use_counting == False:
        num_states  = 2047
    else:
        num_states  = 16383
    
    num_actions = 2
    
    data = genfromtxt("q_matrix.csv", delimiter=",")
    
    q_matrix = data.reshape(num_states, num_actions)
    
    player_type = Hand.HARD
    card_count  = 7

    hit_vector = []
    stand_vector = []
    
    # 5 to 21 because 4 is a pair, and not hard
    # the +1 is because range is not inclusive on the end number.
    for player_value in range(5 - 4, 21 + 1 - 4):
        for dealer_value in range(2 - 2, 11 + 1 - 2):
            state = Map.createRawState(player_value, dealer_value, player_type)
            if use_counting == True:
                state = state << 3
                state = state | card_count
            hit_vector.append(q_matrix[state][Actions.HIT])
            stand_vector.append(q_matrix[state][Actions.STAND])
            print str(q_matrix[state][Actions.HIT]) + ",\t" \
                + str(q_matrix[state][Actions.STAND]) + ", ",
                
        print ""

    
    hit_matrix = np.reshape(hit_vector,(17,10))
    stand_matrix = np.reshape(stand_vector,(17,10))
    
    policy_vector = []
    for i in hit_vector:
        for j in stand_vector:
            if stand_vector.index(j) == hit_vector.index(i):
                if i < j:
                    policy_vector.append(1)
                if i > j:
                    policy_vector.append(0)
    
    policy = np.reshape(policy_vector,(17,10))
#     print policy_vector 
#     print stand_vector
    
    p = vis()
    p.subs(hit_matrix,stand_matrix,policy)

#     h = win32com.client.Dispatch('scratchpad.m')
#     h.Execute ("plot([0 18], [7 23])")
#     h.Execute ("1+1")